#!/bin/bash
#
# Copyright(c) 2012-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#

# The line below specified that line under it should be used as the test's short description when launching test via run_tests script.
# The text should not be longer than 80 chars - if it is, the script will strip addititonal characters
# DESCRIPTION Setup CAS with valid and invalid IDs

# USE_IN_NIGHTLY

# Standard beginning for every test - get the main tests directory and
# link the cas_lib file for CAS API, then use "start_test $*" to pass params
# and do other necessary checks and setup
TESTS_DIR="$(dirname $0)/../"
. $TESTS_DIR/cas_lib
start_test $*

# This is where the real test starts

# Specify "arrays" of IDs used in test
POSITIVE_IDS="1 9238 16384"
NEGATIVE_IDS="-1 abc 16385"

# Use CACHE_DEVICE provided by configuration file and remove partitions from this device
TARGET_DEVICE_OPTION="$CACHE_DEVICE" remove_partitions
# Create 1 primary partition on CACHE_DEVICE of 2000M size
TARGET_DEVICE_OPTION="$CACHE_DEVICE" PARTITION_SIZE_OPTION="2000M" PARTITION_IDS_OPTION="1" make_primary_partitions

# Try to start positive caches in loop and later stop them - if any of those operations fails, it
# means the cache ID is invalid
for ID in $POSITIVE_IDS ; do
    CACHE_ID_OPTION="$ID" CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part1" CACHE_FORCE_OPTION="yes" start_cache
    CACHE_ID_OPTION="$ID" stop_cache
done

# Try to start negative caches in loop - if any of those operations succeeds, it
# means the cache ID is valid (and it shouldn't be). Note that we can't provide an empty
# ID, because the framework will treat it as an undefined option and will fail the test
# automatically.
for ID in $NEGATIVE_IDS ; do
    NEGATIVE_TEST_OPTION="1" CACHE_ID_OPTION="$ID"
        CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part1" CACHE_FORCE_OPTION="yes" start_cache
done

# Always return 0 at the end of the test - if at any point something has failed
# in the API functions, test will end and return a proper result.
# If you need to check other things during the test and end the test earlier, you
# should end the test using "end_test $retval" function
end_test 0
